package com.tddexample;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.inject.Inject;

@ContentView(R.layout.main)
public class TddExampleActivity extends RoboActivity {
	@InjectView(R.id.bnGenerateNumbers)
	protected Button bnGenerateNumbers;
	@InjectView(R.id.pbMax)
	protected ProgressBar pbMax;
	@InjectView(R.id.pbMin)
	protected ProgressBar pbMin;

	@Inject
	INumberGenerator numberGenerator;

	private final int NO_NUMBERS = 5;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public void onClickGenerateNumbers(View view) {
		int[] numbers = this.numberGenerator.getNumbers(this.NO_NUMBERS);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < numbers.length; i++) {
			sb.append(numbers[i] + " ");
		}

		Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();

		this.pbMax.setProgress(this.max(numbers));
		this.pbMin.setProgress(this.min(numbers));

	}

	private int max(int[] numbers) {
		int max = numbers[0];
		for (int i = 1; i < numbers.length; i++)
			if (numbers[i] > max)
				max = numbers[i];
		return max;
	}

	private int min(int[] numbers) {
		int min = numbers[0];
		for (int i = 1; i < numbers.length; i++)
			if (numbers[i] < min)
				min = numbers[i];
		return min;
	}
}